import pandas as pd
import warnings
import datetime
import numpy as np
warnings.filterwarnings('ignore')

A = pd.read_csv('data/train_rice1.csv', header=0, encoding='gbk')
cols = ['区县id', '2015年早稻', '2015年晚稻', '2016年早稻', '2016年晚稻', '2017年早稻', '2017年晚稻']
A = A.ix[:, cols]
counties = A.ix[:, 0]
B = pd.read_csv('data/train_weather1.csv', header=0, encoding='gbk')
B.insert(2, 'date', B.apply(lambda x: datetime.date(x[2], x[3], x[4]).strftime("%Y%m%d"), axis=1))
B.drop(['年份', '月份', '日期'], axis=1, inplace=True)


# Cannot do inplace boolean setting on mixed-types with a non np.nan value
# B.where(B == '*', 0, inplace=True)
B.replace({'*': '0', '/': '0'}, inplace=True)
cols = ['区县id', '站名id', 'date', '日照时数（单位：h)', '日平均风速(单位：m/s)', '日降水量（mm）', '日最高温度（单位：℃）', '日最低温度（单位：℃）',
        '日平均温度（单位：℃）', '日相对湿度（单位：%）', '日平均气压（单位：hPa）']
B1 = B.ix[:, cols]
B1.ix[:, 1] = B1.ix[:, 1].astype(np.object)
B1.ix[:, 3:] = B1.ix[:, 3:].astype(np.float64)

data = []
label = []
num1 = 2 #2, 5(训练)

j= -1
for month in [['0401', '0701'], ['0615', '1015']]:
    j += 1
    i = 0
    for year in ['2015', '2016', '2017', '2018']:
        B2015 = B1.ix[(B['date'] > '{year}{month}'.format(year=year, month=month[0]))
                      & (B['date'] < '{year}{month}'.format(year=year, month=month[1])), :]
        B2015grp = B2015.groupby('区县id')
        B2015cnt = B2015grp.describe()
        cols_sp = list(range(0, 64, 8))
        cols_all = list(range(0, 64, 1))
        cols_diff = [a for a in cols_all if a not in cols_sp]
        B2015fea0 = B2015cnt.ix[:, cols_diff]
        B2015fea = B2015fea0.ix[counties, :]
        data += [B2015fea]
        i += 1
        if i > num1+1:
            1
        elif i > 1:
            label += [A.ix[:, 2*i-1+j]-A.ix[:, 2*i-3+j]]

X_0 = pd.concat(data[1:3]+data[5:7])
y = pd.concat(label[0:2]+label[2:4])

X_0.to_csv('data/8all_fea_season.csv', header=False, index=False)

